<?php

namespace app\api\library;

class Tools
{
    public function params()
    {


   //     $data =$GLOBALS['HTTP_RAW_POST_DATA'];

        $content = file_get_contents("php://input");


     //   var_dump($content);die();

        $xml =simplexml_load_string($content); //xml转object

        $xml= json_encode($xml); //objecct转json

        $xml=json_decode($xml,true); //json转array

//        $dom = new \DOMDocument();
//
//        $dom->load($content);
//
//
//        $root = $dom->documentElement();
        return $xml;

     }


    public function xmlToJson($xml,$root='Response')
    {
        $xml =simplexml_load_string($xml); //xml转object

        $xml= json_encode($xml); //objecct转json

        $xml= json_decode($xml,true); //json转array
        return json_encode([$root=>$xml]);
    }


    public function xmlReturn($data= [


    ],$root='<Response/>')
    {

        $xml = new \SimpleXMLElement($root);
        foreach ($data as $key => $value) {

            $xml->addChild($key, $value);
        }
        return $xml->asXML();

     }
     public function xmlReturn1($data= [


    ],$root='<Response/>')
    {


        return '<?xml version="1.0" encoding="UTF-8" ?>'."\n".$data;

     }
}